/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.coopeud.vista.registro;

import edu.coopeud.persistencia.dao.AsistenciaDao;
import edu.coopeud.persistencia.dao.EventoDao;
import edu.coopeud.persistencia.dao.InscripcionDao;
import edu.coopeud.persistencia.dao.PersonaDao;
import edu.coopeud.persistencia.model.Asistencia;
import edu.coopeud.persistencia.model.Evento;
import edu.coopeud.persistencia.model.Inscripcion;
import edu.coopeud.persistencia.model.Persona;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Alejo
 */
public class VentanaRealizarInscripcion extends javax.swing.JFrame {

    private List<Evento> eventos ;
    private List<Persona> personas ;
    private VentanaRegistro registro;
    
    public VentanaRealizarInscripcion() {
        super("Inscribirse");
        initComponents();
        
        this.eventos = EventoDao.getInstanciaEventoDao().obtenerEventos();
        
        System.out.println("obtenerEventos: "+this.eventos);
        
        for (Evento evento : this.eventos) {
            
            cbxEvento.insertItemAt(evento.getnNombreEvento(), eventos.indexOf(evento));
            
        }
        
        this.personas = PersonaDao.getInstanciaPersonaDao().obtenerPersonas();
        
        System.out.println("obtenerPersonas: "+this.personas);
        
        for (Persona persona : this.personas) {
            
            cbxPersonas.insertItemAt(persona.getnNombre()+" "+persona.getnApellido(), personas.indexOf(persona));
            
        }
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblUno = new javax.swing.JLabel();
        cbxEvento = new javax.swing.JComboBox();
        btnInscribir = new javax.swing.JButton();
        cbxPersonas = new javax.swing.JComboBox();
        labelPersona = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lblUno.setText("Seleccione el Evento");

        btnInscribir.setText("Inscribirse");
        btnInscribir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInscribirActionPerformed(evt);
            }
        });

        labelPersona.setText("Persona");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblUno, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(labelPersona, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(btnInscribir)
                        .addContainerGap(198, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cbxPersonas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbxEvento, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUno)
                    .addComponent(cbxEvento, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxPersonas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPersona))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnInscribir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cerrar();
        getRegistro().setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void btnInscribirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInscribirActionPerformed
        
        Inscripcion inscripcion = new Inscripcion();
        inscripcion.setfInscripcion(new Date());
        inscripcion.setiInscripcionPaga("N");
        
        int pkCodInscripcion = InscripcionDao.getInscripcionDao().crearInscripcion(inscripcion);
        
        Evento evento = this.eventos.get(cbxEvento.getSelectedIndex());
        Persona persona = this.personas.get(cbxPersonas.getSelectedIndex());
        
        Asistencia asistencia = new Asistencia();
        asistencia.setPkCodEvento(evento.getPkCodEvento());
        asistencia.setPkCodInscripcion(pkCodInscripcion);
        asistencia.setPkDocumento(persona.getPkDocumento());
        
        int respuestaAsistencia = AsistenciaDao.getInstanciaAsistenciaDao().insertarAsistencia(asistencia);
        
        System.out.println("realizarInscripcion: pkCodEvento->"+evento.getPkCodEvento()+"; respuesta crearInscripcion pkCodInscripcion: "+pkCodInscripcion+"; respuesta insertarAsistencia: "+respuestaAsistencia);
        
    }//GEN-LAST:event_btnInscribirActionPerformed

    public void cerrar(){
        dispose();
    }
    
    public VentanaRegistro getRegistro (){
        if (registro==null){
            registro=new VentanaRegistro();
        }
        return registro;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInscribir;
    private javax.swing.JComboBox cbxEvento;
    private javax.swing.JComboBox cbxPersonas;
    private javax.swing.JLabel labelPersona;
    private javax.swing.JLabel lblUno;
    // End of variables declaration//GEN-END:variables
}
